package com.jielihaofeng.netty.bytebuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;

import static com.jielihaofeng.netty.bytebuf.TestByteBuf.log;

/**
 * @author Johnnie Wind
 * @description 测试 ByteBuf duplicate()复制
 * @date 2021/11/11 17:13
 */
public class TestDuplicate {

    public static void main(String[] args) {
        ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(10);
        buf.writeBytes(new byte[]{'a','b','c','d','e','f','i','j','k','l'});
        // 复制buf
        ByteBuf newBuf = buf.duplicate();
        System.out.println("复制buf........................................");
        log(buf);
        log(newBuf);

        // 修改newBuf
        newBuf.setByte(0,'x');
        System.out.println("修改newBuf........................................");
        log(buf);
        log(newBuf);

        // newBuf写入一个字节
        newBuf.writeByte('m');
//        buf.writeByte('n');
        System.out.println("newBuf写入一个字节........................................");
        log(buf);
        log(newBuf);

        // buf读取一个字节
        buf.readByte();
        System.out.println("buf读取一个字节........................................");
        log(buf);
        log(newBuf);
    }
}
